package com.cloudcc.mobile.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.db.MessageDB;
import com.cloudcc.mobile.entity.Message;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.view.activity.BeauInfoActivity;
import com.cloudcc.mobile.view.web.NewWebView;

/**
 * 系统消息适配器
 * 
 * @author Administrator
 * 
 */
public class MessageAdapter extends BaseAdapter {
	private List<Message> list;
	private Context context;

	public MessageAdapter(Context context, List<Message> list) {
		this.list = list;
		this.context = context;
	}

	public void changeData(List<Message> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (list.size()>20){
			return 20;
		}
		return list.size();
	}

	@Override
	public Message getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.message_list_item, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.context = (TextView) convertView.findViewById(R.id.context);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			convertView.setTag(holder);
			
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final Message message = getItem(position);
		holder.name.setText(message.getName());
		holder.time.setText(message.getTime());
		try {
			final String []list = message.getContext().split("/");
			holder.context.setText(list[0]);
			holder.context.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(context, BeauInfoActivity.class);
					try {
						
					
					String id = list[1];
					intent.putExtra("web", id);
					if((id.startsWith("bef"))||((id.startsWith("bfa")))){
						intent.putExtra("web.url",UrlManager.getRootUrl()+ UrlTools.getObjectUrl_z(id));
					}else{
						intent.putExtra("web.url",UrlManager.getRootUrl()+ UrlTools.getObjectUrl(id));
					}		
					context.startActivity(intent);
					MessageDB db = new MessageDB(context, UserManager.getManager().getUser().userId
							+ "message");
					db.deleteById(Message.class, message.getId());
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			});
		} catch (Exception e) {
			// TODO: handle exception
			holder.context.setText(message.getContext());
		}
		
		return convertView;
	}

	class ViewHolder {
		TextView name;
		TextView time;
		TextView context;

	}
}
